<?php

namespace App\GraphQL\Queries\FactoryDevice;

use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use App\Models\FactoryDevice;

class QueryFactoryDevices
{
    /**
     * Return a value for the field.
     *
     * @param  null  $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param  mixed[]  $args The arguments that were passed into the field.
     * @param  \Nuwave\Lighthouse\Support\Contracts\GraphQLContext  $context Arbitrary data that is shared between all fields of a single query.
     * @param  \GraphQL\Type\Definition\ResolveInfo  $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        // TODO implement the resolver
        $authUser = auth()->user();
        $role = $authUser->role;
        $query = FactoryDevice::query();
        if($role->code=='FACTORY'){
            $query->where('user_id',$authUser->id);
        }
        if($args['status']){
            $query->where('status',$args['status']);
        }
        $result = $query->paginate($args['limit'] ,['*'],'page', $args['page']);
        return [
            'data'=>$result->items(),
            'paginatorInfo'=>[
                'count'=>$result->count(),
                'currentPage'=>$result->currentPage(),
                'firstItem'=>$result->firstItem(),
                'hasMorePages'=>$result->hasMorePages(),
                'lastItem'=>$result->lastItem(),
                'lastPage'=>$result->lastPage(),
                'perPage'=>$result->perPage(),
                'total'=>$result->total()
            ]
        ];
    }
}
